package br.ita.trucocearense.server.core.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import br.ita.trucocearense.server.core.jpa.JPAUtil;
import br.ita.trucocearense.server.domain.Usuario;

public class UsuarioDaoImpl implements UsuarioDao{

	@Override
	public Usuario getByLogin(String login) {
		EntityManager em = JPAUtil.getEntityManager();
		Usuario user = em.find(Usuario.class, login);
		em.close();
		return user;
	}

	@Override
	public void insert(Usuario user) {
		EntityManager em = JPAUtil.getEntityManager();
		EntityTransaction t = em.getTransaction();
		t.begin();
		em.persist(user);
		t.commit();
		em.close();
	}

	@Override
	public void update(Usuario user) {
		EntityManager em = JPAUtil.getEntityManager();
		EntityTransaction t = em.getTransaction();
		t.begin();
		em.merge(user);
		t.commit();
		em.close();
	
	}

	@Override
	public void delete(Usuario user) {
		EntityManager em = JPAUtil.getEntityManager();
		EntityTransaction t = em.getTransaction();
		t.begin();
		user = em.merge(user);
		em.remove(user);
		t.commit();
		em.close();
	}



}
